package com.masterclient.app.modules.aftersale.contract;

import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.aftersale.CommentProductBean;
import com.eoner.baselibrary.bean.personal.UploadImgBean;
import com.google.gson.Gson;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/08/08
*/
public class CommentPresenter extends CommentContract.Presenter{

    @Override
    public void getCommentProduct(String trade_no) {
        Observable<CommentProductBean> observable = RetrofitUtil.getInstance().getCommentProduct(trade_no);
        Disposable disposable = toSubscribe(observable, new Consumer<CommentProductBean>() {
            @Override
            public void accept(@NonNull CommentProductBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getCommentProducts(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }


    @Override
    public void uploadImage(File image, String type,int mPosition) {


        Map<String, RequestBody> map = new HashMap<>();
        RequestBody requestBody1 =  RequestBody.create(MediaType.parse("multipart/form-data"), image);

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        RequestBody imageBody = RequestBody.create(MediaType.parse("image/png"), image);
        builder.addFormDataPart("imgfile", "123.jpeg", imageBody);//imgfile 后台接收图片流的参数名
        builder.addFormDataPart("type",type);

        Observable<UploadImgBean> observable = RetrofitUtil.getInstance().uploadImage(builder.build().parts());
        Disposable disposable = toSubscribe(observable, new Consumer<UploadImgBean>() {
            @Override
            public void accept(@NonNull UploadImgBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.onUploadSuccess(bean.getData(),mPosition);
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void addOrderReview(Map<String, String> content, Map<String, List<String>> image, String order_no, Map<String, String> rating_level) {

        Gson gson = new Gson();
        String mcontent = gson.toJson(content);
        String mImage = gson.toJson(image);
        String mRate = gson.toJson(rating_level);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"),"content="+mcontent+"&image="+mImage+"&order_no="+order_no+"&rating_level="+mRate);
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().addOrderReview(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.success();
                }else {
                    mView.failed("提交评价失败");
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}